package Projo;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JPanel;

import Exception.CheckDataException;
import Exception.InsertionDataException;
import Exception.LoadDriverException;
import General.GraphicsSetting;
import General.TreatError;

public class PanelShow extends JPanel
{	
	private ArrayList<Student> listStudent;
	private JButton buttonStopGeneral;
	private int cpt = 0;
	private int yLogo = -40, yBar = -13, yButtonStart = -13, yButtonPause = -13, yButtonStop = -13;
	private Connection connection;
		
	public PanelShow(ArrayList<Student> listStudent, Connection connection)
	{
		setLayout(null);
		
		this.connection = connection;
		this.listStudent = new ArrayList<Student>(listStudent);
		
		MyActionListener actionListener = new MyActionListener();
		
		buttonStopGeneral = new JButton("StopGeneral");
		buttonStopGeneral.addActionListener(actionListener);
		buttonStopGeneral.setName("StopGeneral");
		buttonStopGeneral.setBounds(0,0,64,28);
		this.add(buttonStopGeneral);
		
		showStudent();
	}
	
	private class MyActionListener implements ActionListener
	{
		public void actionPerformed(ActionEvent arg0)
		{
			try
			{
				if(arg0.getSource() == buttonStopGeneral)
					for(Student student : listStudent)
					{
						student.getThread().end();
						student.getButtonPause().setEnabled(false);
						student.getButtonStart().setEnabled(false);
						student.getButtonStop().setEnabled(false);
					}
				buttonStopGeneral.setEnabled(false);
			}
			catch(CheckDataException | InsertionDataException | LoadDriverException error)
			{
				TreatError.showError(error);
			}
		}
	}
	
	public void showStudent()
	{		
		for(cpt = 0; cpt < this.listStudent.size(); cpt++)
		{
			listStudent.get(cpt).getLogoStudent().setBounds(30, yLogo += 90, 50, 70);
			this.add(listStudent.get(cpt).getLogoStudent());
			listStudent.get(cpt).getProgressBar().setBounds(110, yBar += 90, 450, 30);
			this.add(listStudent.get(cpt).getProgressBar());
			listStudent.get(cpt).getButtonStart().setBounds(570, yButtonStart += 90, 64, 28);
			this.add(listStudent.get(cpt).getButtonStart());
			listStudent.get(cpt).getButtonPause().setBounds(640, yButtonPause += 90, 64, 28);
			this.add(listStudent.get(cpt).getButtonPause());
			listStudent.get(cpt).getButtonStop().setBounds(710, yButtonStop += 90, 64, 28);
			this.add(listStudent.get(cpt).getButtonStop());
		}
		
		GraphicsSetting.setBackground(this, "image/Bg-Projo.jpg", 0, 0, 800, 1000);		// image/main.png
	}
	
	public ArrayList<Student> getListStudent(){return this.listStudent;}
}
